package com.mindata.blockchain.common;

import com.mindata.blockchain.ApplicationContextProvider;
import com.mindata.blockchain.core.bean.Member;
import com.mindata.blockchain.core.manager.DbBlockManager;
import com.mindata.blockchain.socket.client.ClientStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.tio.core.Node;

import javax.annotation.Resource;
import java.util.*;

@Component
public class Commonvalue {
    private int nodeIndex;
    private int mygroupid;
    private int groupsize;
    private int AgreeSize;
    @Value("#{T(java.lang.Integer).parseInt(${blocklimit})}")
    private int blockTxLimit;
    private int leaderKvalue = 4;
    private int globalAgreeSize;
    private int shardnum;
    private Set<Member> globalNodes = new HashSet<>();
    private Map<Integer, List<Member>> group_memberMap = new HashMap<>();
    private Map<Integer, Map<Integer, Node>> leaderfilter = new HashMap<>();
    private Map<Node, Integer> member_groupMap = new HashMap<>();
    @Value("${singeNode}")
    private boolean isSingle;
    @Resource
    private DbBlockManager dbBlockManager;
    private Logger logger = LoggerFactory.getLogger(getClass());

    public Set<Member> getGlobalNodes() {
        return globalNodes;
    }

    public int getAgreeSize() {
        return AgreeSize;
    }

    public void setAgreeSize(int agreeSize) {
        AgreeSize = agreeSize;
    }

    public void setGlobalNodes(Set<Member> globalNodes) {
        this.globalNodes = globalNodes;
    }

    public int getGroupsize() {
        return groupsize;
    }

    public void setGroupsize(int groupsize) {
        this.groupsize = groupsize;
        int pbfnodeNum = (groupsize - 1) / 3;
        if (groupsize >= 4) {
            setAgreeSize(pbfnodeNum * 2 + 1);
        } else {
            setAgreeSize(groupsize);
        }
    }

    public Map<Integer, Map<Integer, Node>> getLeaderfilter() {
        return leaderfilter;
    }

    public void setLeaderfilter(Map<Integer, Map<Integer, Node>> leaderfilter) {
        this.leaderfilter = leaderfilter;
    }

    public Map<Integer, List<Member>> getGroup_memberMap() {
        return group_memberMap;
    }

    public void setGroup_memberMap(Map<Integer, List<Member>> group_memberMap) {
        this.group_memberMap = group_memberMap;
        setGroupsize(group_memberMap.get(getMygroupid()).size());
    }

    public int getMygroupid() {
        return mygroupid;
    }

    public void setMygroupid(int mygroupid) {
        this.mygroupid = mygroupid;
    }

    public int getShardnum() {
        return shardnum;
    }

    public void setShardnum(int shardnum) {
        if (shardnum > 0) {
            this.shardnum = shardnum;
        } else {
            this.shardnum = shardnum;
        }
        if (shardnum >= 4) {
            setGlobalAgreeSize((shardnum - 1) / 3);
        } else {
            setGlobalAgreeSize(shardnum);
        }
    }

    public int getNodeIndex() {
        return nodeIndex;
    }

    public void setNodeIndex(int nodeIndex) {
        this.nodeIndex = nodeIndex;
    }

    public int getBlockTxLimit() {
        return blockTxLimit;
    }

    public void setBlockTxLimit(int blockTxLimit) {
        this.blockTxLimit = blockTxLimit;
    }

    public int getLeaderKvalue() {
        return leaderKvalue;
    }

    public void setLeaderKvalue(int leaderKvalue) {
        this.leaderKvalue = leaderKvalue;
    }

    public int getGlobalAgreeSize() {
        return globalAgreeSize;
    }

    public void setGlobalAgreeSize(int globalAgreeSize) {
        this.globalAgreeSize = globalAgreeSize;
    }

    public Map<Node, Integer> getMember_groupMap() {
        return member_groupMap;
    }

    public void setMember_groupMap(Map<Node, Integer> member_groupMap) {
        this.member_groupMap = member_groupMap;
    }

    @Override
    public String toString() {
        return "Commonvalue{" +
                "nodeIndex=" + nodeIndex +
                ", mygroupid=" + mygroupid +
                ", groupsize=" + groupsize +
                ", AgreeSize=" + AgreeSize +
                ", blockTxLimit=" + blockTxLimit +
                ", globalAgreeSize=" + globalAgreeSize +
                ", shardnum=" + shardnum +
                ", isSingle=" + isSingle +
                '}';
    }
}
